#ifndef I2C_H
#define I2C_H

#include <QObject>
#include <QDebug>

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stropts.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

//! Max length of the name to the I2C bus file location
#define MAX_BUS 64

class I2C {
public:
  /*! \brief Constructor for the I2C bus class
   *  \param i2cBusNr The I2C bus interface we wish to use */
  I2C(qint8 i2cBusNr);

  /*! \brief Write a set of data to an I2C device
   *  \param devAddr The address of the device we wish to write to
   *  \param address The start address of where we want to place the data
   *  \param length The length of the data we with to write
   *  \param data The data we wish to write to the I2C device
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to write values
   *  TODO: Not tested */
  qint8 writeI2CDevice(quint8 devAddr, quint8 address, quint8 length, qint8 *data);

  /*! \brief Write a byte to an I2C device
   *  \param devAddr The address of the device we wish to write to
   *  \param address The address of where we want to place the byte
   *  \param value The data we wish to write to the I2C device
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to write values */
  qint8 writeI2CDevice(quint8 devAddr, quint8 address, quint8 value);

  /*! \brief Write to an I2C device
   *  \param devAddr The address of the device we wish to write to
   *  \param address The address which we wish to send to the I2C device
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to write values */
  qint8 writeI2CDevice(quint8 devAddr, quint8 address);

  /*! \brief Read from an I2C device
   *  \param devAddr The address of the device we wish to read from
   *  \param data Where we wish to store the result from the read (pointer)
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to read values */
  qint8 readI2CDevice(quint8 devAddr, qint8 *data);

  /*! \brief Read from an I2C device
   *  \param devAddr The address of the device we wish to read from
   *  \param address The address where we wish to read the value from
   *  \param data Where we wish to store the result from the read (pointer)
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to read values */
  qint8 readI2CDevice(quint8 devAddr, quint8 address, qint8 *data);

  /*! \brief Read a chunk of data from an I2C device
   *  \param devAddr The address of the device we wish to read from
   *  \param address The address where we wish to read the value from
   *  \param length The number of bytes we wish to read
   *  \param data Where we wish to store the result from the read (pointer)
   *  \return 0 = SUCCESS, 1 = Failed to open HW interface,
   *          2 = No response from device, 3 = Failure to read values
   *  TODO: Not tested */
  qint8 readI2CDevice(quint8 devAddr, quint8 address, quint8 length, qint8 *data);
private:
  //! The HW interface we wish to use
  qint8 i2cBusNr;
};

#endif // I2C_H
